package com.fmi.timedealer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import android.widget.AdapterView.OnItemClickListener;

import com.fmi.timedealer.achievement.AchievementsActivity;
import com.fmi.timedealer.datamanager.IDataManager;
import com.fmi.timedealer.datamanager.SQLiteDataManager;
import com.fmi.timedealer.moodtracking.Mood;
import com.fmi.timedealer.moodtracking.MoodManagementActivity;
import com.fmi.timedealer.notifications.PlansNotificationManager;
import com.fmi.timedealer.plan.PlansActivity;
import com.fmi.timedealer.track.TrackTimeActivity;

/**
 * Main screen, activated by starting of the application
 */
public class TimeDealerActivity extends Activity {
	
	private IDataManager dbManager;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		dbManager = new SQLiteDataManager("MyDB", this);
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_screen);
		
		PlansNotificationManager.getInstance().setNotification(getApplicationContext());
		
		ImageView moodIcon = (ImageView) findViewById(R.id.mood_icon);
		moodIcon.setImageResource(getMoodByCode(dbManager.getCurrentMoodCode()).getIcon());
		
		TextView lblCurrentMood = (TextView) findViewById(R.id.mood_label);
		lblCurrentMood.setText(getMoodByCode(dbManager.getCurrentMoodCode()).getLabel());
	
		GridView gridview = (GridView) findViewById(R.id.gridview);
		gridview.setAdapter(new MyGridAdapter());
		gridview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				
				switch (position) {
				case (0): {
					Intent i = new Intent(TimeDealerActivity.this, TrackTimeActivity.class);
					startActivity(i);
					break;
				}
				case (1): {
					Intent i = new Intent(TimeDealerActivity.this, MoodManagementActivity.class);
					startActivity(i);
					break;
				}
				case (2): {
					Intent i = new Intent(TimeDealerActivity.this, PlansActivity.class);
					startActivity(i);
					break;
				}
				case (3): {
					Intent i = new Intent(TimeDealerActivity.this, AchievementsActivity.class);
					startActivity(i);
					break;
				}
				default:
				}
			}
		});
	}

	private class MyGridAdapter extends BaseAdapter{
		
		public MyGridAdapter() {
			// TODO Auto-generated constructor stub
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return 4;
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			
			View v = convertView;
			
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.toolitem_row, null);
            }
            
            String text = null;
            int imageRsource = -1;

			switch (position) {
			case (0): {
				imageRsource = R.drawable.second;;
            	text = "Track";
            	break;
			}
			case (1): {
				text = "Mood";
            	imageRsource = R.drawable.third;
            	break;
			}
			case (2): {
				text = "Plan";
            	imageRsource = R.drawable.first;
            	break;
			}
			case (3): {
				text = "Achievement";
            	imageRsource = R.drawable.fourth;
            	break;
			}
			default:
			}
            
            ImageView imageView = (ImageView) v.findViewById(R.id.image);
            imageView.setImageResource(imageRsource);
            
            TextView tvName = (TextView)v.findViewById(R.id.text);
            tvName.setText(text);       
            
            return v;
		}
	}
	
	private Mood getMoodByCode(int code) {
		
		Mood mood = null;
		for(Mood current : Mood.values()) {
			if(current.getCode() == code) {
				return current;
			}
		}
		return mood;
	}
	
	@Override
	public void onResume()
	{
		super.onResume();

		ImageView moodIcon = (ImageView) findViewById(R.id.mood_icon);
		moodIcon.setImageResource(getMoodByCode(dbManager.getCurrentMoodCode()).getIcon());
		
		TextView lblCurrentMood = (TextView) findViewById(R.id.mood_label);
		lblCurrentMood.setText(getMoodByCode(dbManager.getCurrentMoodCode()).getLabel());
	 }
}